from django.db import models
from django.utils.translation import ugettext_lazy as _
import datetime

class Entry(models.Model):
    """A friendfeed entry"""
    ff_id       = models.CharField(_('ff_id'), blank=True, max_length=100)
    user        = models.ForeignKey('User')
    title       = models.CharField(_('title'), blank=True, max_length=255)
    link        = models.URLField(_('link'), blank=True, verify_exists=False, max_length=500)
    published   = models.DateTimeField(_('published'), blank=True, default=datetime.datetime.now)
    updated     = models.DateTimeField(_('updated'), blank=True, default=datetime.datetime.now)
    hidden      = models.BooleanField(_('hidden'), default=False)
    anonymous   = models.BooleanField(_('anonymous'), default=False)
    service     = models.ForeignKey('Service')
    via         = models.CharField(_('via'), blank=True, max_length=100)

    @property
    def num_comments(self):
        return self.comment_set.all().count()
        
    @property
    def num_likes(self):
        return self.like_set.all().count()
     
    @property
    def num_media(self):
        return self.media_set.all().count()
        
    class Admin:
        list_display = ('ff_id', 'title', 'user', 'service', 'published', 'num_comments', 'num_likes', 'num_media')
        search_fields = ('title',)
        list_filter = ('service', 'published')
        
    class Meta:
        verbose_name = _('entry')
        verbose_name_plural = _('entries')
        ordering = ['-published',]
        get_latest_by = 'published'

    def __unicode__(self):
        return u"%s" % (self.title)
        
class User(models.Model):
    """A friendfeed user"""
    ff_id       = models.CharField(_('ff_id'), blank=True, max_length=100)
    name        = models.CharField(_('name'), blank=True, max_length=255)
    nickname    = models.CharField(_('nickname'), blank=True, max_length=255)
    profileURL  = models.URLField(_('profileURL'), blank=True, verify_exists=False)

    class Admin:
        list_display = ('name','nickname', 'id')
        search_fields = ('name', 'nickname')
        
    class Meta:
        verbose_name = _('user')
        verbose_name_plural = _('users')

    def __unicode__(self):
        return u"%s" % (self.name,)

class Service(models.Model):
    """A friendfeed service"""
    ff_id       = models.CharField(_('ff_id'), blank=True, max_length=100)
    name        = models.CharField(_('name'), blank=True, max_length=100)
    iconURL     = models.URLField(_('iconURL'), blank=True, verify_exists=False)
    profileURL  = models.URLField(_('profileURL'), blank=True, verify_exists=False)

    class Admin:
        list_display = ('name', 'ff_id')
        search_fields = ('name', 'ff_id',)

    class Meta:
        verbose_name = _('service')
        verbose_name_plural = _('services')

    def __unicode__(self):
        return u"%s" % (self.name)

## Comment
class Comment(models.Model):
    """A friendfeed comment"""
    ff_id       = models.CharField(_('ff_id'), blank=True, max_length=100)
    entry       = models.ForeignKey(Entry, edit_inline=models.STACKED, num_extra_on_change=0)
    date        = models.DateTimeField(_('date'), blank=True, default=datetime.datetime.now)
    user        = models.ForeignKey(User)
    body        = models.TextField(_('body'), blank=True, core=True)

    class Meta:
        verbose_name = _('comment')
        verbose_name_plural = _('comments')

    def __unicode__(self):
        return u"%s" % (self.body)
        
## Media
class Media(models.Model):
    """A friendfeed media"""
    entry       = models.ForeignKey(Entry, edit_inline=models.TABULAR, num_extra_on_change=0)
    title       = models.CharField(_('title'), blank=True, max_length=255, null=True, core=True)
    player      = models.CharField(_('player'), blank=True, null=True, max_length=255)
    
    class Admin:
        list_display = ('title', 'player')
        search_fields = ('title', 'player')

    class Meta:
        verbose_name = _('media')
        verbose_name_plural = _('media')
        ordering = ['-id',]

    def __unicode__(self):
        return u"%s" % (self.title,)

## Content
class MediaContent(models.Model):
    """A friendfeed media contents"""
    media       = models.ForeignKey(Media, edit_inline=models.STACKED, num_extra_on_change=0)
    url         = models.URLField(_('url'), blank=True, verify_exists=False, core=True)
    type        = models.CharField(_('type'), blank=True, null=True, max_length=100)
    width       = models.IntegerField(_('width'), blank=True, null=True)
    height      = models.IntegerField(_('height'), blank=True, null=True)

    class Meta:
        verbose_name = _('mediacontent')
        verbose_name_plural = _('mediacontent')

    def __unicode__(self):
        return u"%s" % (self.url,)

## Thumbnail
class MediaThumbnail(models.Model):
    """Thumbnails for a bit of Media"""
    media       = models.ForeignKey(Media, edit_inline=models.STACKED, num_extra_on_change=0)
    url         = models.URLField(_('url'), blank=True, verify_exists=False, core=True)
    width       = models.IntegerField(_('width'), blank=True, null=True)
    height      = models.IntegerField(_('height'), blank=True, null=True)

    class Meta:
        verbose_name = _('mediathumbnail')
        verbose_name_plural = _('medialthumbnails')

    def __unicode__(self):
        return u"%s" % (self.url,)

## Like
class Like(models.Model):
    """A friendfeed like"""
    entry       = models.ForeignKey(Entry, edit_inline=models.STACKED, num_extra_on_change=0)
    date        = models.DateTimeField(_('date'), blank=True, default=datetime.datetime.now)
    user        = models.ForeignKey(User, core=True)

    class Meta:
        verbose_name = _('like')
        verbose_name_plural = _('likes')

    def __unicode__(self):
        return u"%s likes %s" % (self.user, self.entry)